package com.example.controller;

import com.example.service.NoticeService;
import com.example.common.Result;
import com.example.entity.Notice;
import com.example.entity.Params;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/notice")
public class NoticeController {

    private static final Logger log = LoggerFactory.getLogger(NoticeController.class);

    @Resource
    private NoticeService noticeService;

    @GetMapping
    public Result findAll() {
        List<Notice> list = noticeService.findAll();
        return Result.success(list);
    }

    @GetMapping("/search")
    public Result findBySearch(Params params) {
        log.info("调用接口查询公告信息");
        PageInfo<Notice> info = noticeService.findBySearch(params);
        return Result.success(info);
    }

    @PostMapping
    public Result save(@RequestBody Notice notice) {
        if (notice.getNoticeId() == null) {
            noticeService.add(notice);
        } else {
            noticeService.update(notice);
        }
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer noticeId) {
        noticeService.delete(noticeId);
        return Result.success();
    }
}
